<template>
  <div class="app-container">
    <!-- <div class="button-area">
      <el-button type="primary" @click="dialogFormVisible = true"
        >入库</el-button
      >
      <el-button type="primary" @click="dialogFormVisible = true"
        >出库</el-button
      >
    </div> -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="名称"> </el-table-column>
      <el-table-column prop="typeName" label="类型"> </el-table-column>
      <el-table-column prop="bandName" label="品牌"> </el-table-column>
      <el-table-column prop="sku" label="规格"> </el-table-column>
      <el-table-column prop="stock" label="库存数量"> </el-table-column>
      <el-table-column prop="price" label="金额"> </el-table-column>
      <el-table-column prop="action" label="操作">
        <template slot-scope="scope">
          <el-button
            @click="handleClick(scope.row, 'delete')"
            type="text"
            size="small"
            >删除</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="handleClick(scope.row, 'edit')"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="pagesize"
        :current-page="page"
      >
      </el-pagination>
    </div>
    <el-dialog title="入库" :visible.sync="dialogFormVisible" width="80%">
      <el-table :data="selectProductData" style="width: 100%">
        <el-table-column prop="review" label="状态"> </el-table-column>
        <el-table-column prop="buyName" label="采购人"> </el-table-column>
        <el-table-column prop="buyTime" label="采购日期"> </el-table-column>
        <el-table-column prop="name" label="采购单商品"> </el-table-column>
        <el-table-column prop="num" label="*本次入库数量"> </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('buyForm')"
          >入 库</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listReagentStock,
  newReagentStock,
  delReagentStock,
} from '@/api/reagent/stock'
export default {
  name: 'Stock',
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      selectProductData: [],
      form: {
        type: 1,
        name: '',
        deptId: '',
      },
      rules: {
        type: [{ required: true, message: '请输入分类名称', trigger: 'blur' }],
      },
      productRules: {},
      isEdit: false,
      editId: '',
      total: 0,
      page: 1,
      pagesize: 10,
      products: [],
    }
  },
  created() {
    this.getReagentStock()
  },
  methods: {
    getReagentStock() {
      const self = this
      listReagentStock({pageNum: self.page, pageSize: self.pagesize}).then((response) => {
        if (response && response.rows) {
          self.tableData = response.rows
          self.total = response.total;
        }
      })
    },
    submitForm(formName) {
      const self = this
      self.$refs[formName].validate((valid) => {
        if (valid) {
          // TODO
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleClick(row, type) {
      const self = this
      if (type === 'delete') {
        self
          .$confirm('此操作将永久删除该库存, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
          .then(() => {
            delReagentStock(row.id).then((response) => {
              if (response && response.code === 200) {
                self.$message({
                  message: '操作成功',
                  type: 'success',
                })
                self.getReagentStock()
              }
            })
          })
      } else if (type === 'edit') {
        self.isEdit = true
        self.editId = row.id
        self.form = {
          ...row,
        }
        self.dialogFormVisible = true
      }
    },
  },
}
</script>

<style></style>
